// pages/work/daka/index.js
var util = require("../../../utils/util.js")
var my = require("../../../utils/MyUtils.js")
const log=require("../../../utils/log")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    model: {},
    user: null,
    scale: 14,
    date: util.dates(new Date()),
    dakastatus1: true,
    dakastatus2: true,
    nowTime: null,
    juli: 0,
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var day = util.getYmd(new Date())
    var _TOKEN = wx.getStorageSync('_TOKEN')
    var user = wx.getStorageSync('userInfo')

    var that = this
    if (user != null) {
      // user.user_name.length >= 3 ? user.nickName = user.user_name.substr(1, user.user_name.length - 1) : user.nickName = user.user_name

      this.setData({
        user: user
      })
    }
    this.getMyLocation(_TOKEN)
    var ss = setInterval(function () {
      var dk = that.data.dk
      // console.log(dk)
      if (dk != undefined) {
        if (JSON.stringify(dk).indexOf(JSON.stringify(dk.time)) != -1)
          dk.time = util.dates(new Date());
          // dk.statt=true
        that.setData({
          dk: dk
        })
      }
    }, 1000)
    this.setData({
      ss: ss
    })
  },
  onShow: function (options) {
    var hour = util.hour(new Date());
    var message = "";
    if (hour >= 0 && hour <= 12) {
      message = "早安，新的一天请继续加油"
    } else {
      message = "下午好，尽情享受美好时光吧"
    }
    this.setData({
      message: message,
    })
  },
  getMyLocation(TOKEN) {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        my.request("/oa-applet/interval-punch-card", "get", {
          lat: res.latitude,
          lng: res.longitude
        }, {
          token: TOKEN
        }, function (res) {
          
          console.log(res)
          wx.hideLoading()
          // debugger
          if (res.data.code == 0) {
            var context = "打卡"
            var dk = res.data.data
            if(dk.group_type==2){
              context="外出"
            }
            if (res.data.data.attendance_status == 1) {
              if (dk.attendance_diff == 2) {
                context = "外出"
              } else if (dk.attendance_diff == 3) {
                context = "出差"
              }
              wx.setNavigationBarTitle({
                title: context,
              })
              dk.hour = util.hour(new Date())
              dk.min=util.mins(new Date())

              dk.attendance_type==1?dk.hours=dk.attendance_time.substr(0,2) : dk.hours=dk.attendance_time.substr(0,2)
              dk.attendance_type==1?dk.mins=dk.attendance_time.substr(3,2) : dk.mins=dk.attendance_time.substr(3,2)
              dk.time = util.dates(new Date())
              dk.statt=true
              that.setData({
                dk: dk
              })
            } else {
              my.alert("当天未存在排班，如需打卡，请前往签到页面打卡")
            }
          }else{
            my.alert("当天未存在排班，如需打卡，请前往签到页面打卡")
          }
        })
      },
      fail: function (res) {
        wx.getSetting({
          success(res) {
            //若用户没有授权地理位置
            if (!res.authSetting['scope.userLocation']) {
              //在调用需授权 API 之前，提前向用户发起授权请求
              wx.authorize({
                scope: 'scope.userLocation',
                //用户同意授权
                success() {
                  // 用户已经同意小程序使用地理位置，后续调用 wx.getLocation 接口不会弹窗询问
                  that.getMyLocation();
                },
                ////用户不同意授权
                fail() {
                  wx.showModal({
                    title: '提示',
                    content: '此功能需获取位置信息，请授权',
                    success: function (res) {
                      if (res.confirm == false) {
                        return false;
                      }
                      wx.openSetting({
                        success(res) {
                          //如果再次拒绝则返回页面并提示
                          if (!res.authSetting['scope.userLocation']) {
                            wx.showToast({
                              title: '此功能需获取位置信息，请重新设置',
                              duration: 3000,
                              icon: 'none'
                            })
                          } else {
                            //允许授权，调用地图
                            that.onLoad()
                          }
                        }
                      })
                    }
                  })
                }
              })
            } else {
              that.getMyLocation();
            }
          }
        })
      }
    })
  },
  dsbk() {
    var _TOKEN = wx.getStorageSync('_TOKEN')
    var that = this
    var dk = that.data.dk
    dk.statt=false
    clearInterval(this.data.ss)
    wx.getLocation({
      altitude: 'altitude',
      type: 'gcj02',
      success(res) {
        // debugger
        console.log(res.latitude)
        console.log(res.longitude)
        my.request("/oa-applet/interval-punch-func", "post", {
          'lat': res.latitude,
          'lng': res.longitude,
          'attendance_time': dk.attendance_time,
          'attendance_type': dk.attendance_type,
          'into_the_range' : dk.into_the_range
        }, {
          token: _TOKEN
        }, function (ress) {
          // wx.hideLoading()
          wx.hideLoading()
          console.log(ress)
          if (ress.data.code == 0) {
            log.info(my.info(new Date(),that.data.user.user_name))
            let objModal = {
              show: true,
              title: '打卡成功',
              showCancel: false,
              // height: '55%',
              confirmText: '我知道了',
              // url: '../daka/index',
              confirmColor: "#0081DF"
            }
            dk.statt=true
            that.setData({
              modal: objModal,
              dk
            })
            that.onLoad()
          } else {
            my.alerts("此微信账号已打卡，请退出登录", function (sm) {
              if (sm.confirm) {
                wx.removeStorageSync('_TOKEN')
                wx.redirectTo({
                  url: '../../login/login',
                })
              }else{
                return false
              }
            })
          }
        })
      }
    })
  },

  modalOperate() {
    this.onLoad()
  },
  /**
   * 下滑刷新事件
   */
  onPullDownRefresh() {
    //做些什么...
    this.onLoad()
    wx.stopPullDownRefresh() //得到结果后关掉刷新动画

  },
})